package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClientService;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-30
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    /**
     *   根据课程id删除小节
     *   todo 删除小节再删除对应视频文件
     * @param courseId
     */

    @Autowired
    private VodClientService clientService;

    @Override
    public void removeVideoByCourseId(String courseId) {

        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);//必须和课程id相等
        queryWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(queryWrapper);

        //把List<EduVideo>转换为List<String>
        ArrayList<String> videoIds = new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            EduVideo video = videoList.get(i);
            String videoSourceId = video.getVideoSourceId();
            //放到String泛型中的list集合里面
            videoIds.add(videoSourceId);
        }

        //如果有视频，有值再执行删除方法
        if (videoList.size() > 0) {
            //根据多个视频id删除视频
            clientService.deleteBatch(videoIds);
        }


        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        //条件是必须等于课程id才能删除
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
